using System;
using System.Diagnostics;
using AutoMapper;
using Soundteller.Application.Facade.Contracts;
using Soundteller.Application.Facade.Interfaces;
using Soundteller.Audio.Repositories.Interfaces;
using Soundteller.Audio.Services.Interfaces;

namespace Soundteller.Application.Facade
{
    public class AudioFacade : IAudioFacade
    {
        private readonly IMetronome _metronome;
        private readonly ISongLoader _songLoader;
        private readonly ISoundPlayer _soundPlayer;

        public AudioFacade(IMetronome metronome, ISongLoader songLoader, ISoundPlayer soundPlayer)
        {
            if (metronome == null) throw new ArgumentNullException("metronome");
            if (songLoader == null) throw new ArgumentNullException("songLoader");
            if (soundPlayer == null) throw new ArgumentNullException("soundPlayer");

            _metronome = metronome;
            _songLoader = songLoader;
            _soundPlayer = soundPlayer;
        }

        public void TickMetronome(TimeSpan elapsedTime)
        {
            Debug.WriteLine("{0}.TickMetronome( {1} )", GetType().Namespace, elapsedTime);

            _metronome.Tick(elapsedTime);
        }

        public void LoadSong(string filePath)
        {
            Debug.WriteLine("{0}.LoadSong()", GetType().Namespace, null);

            _songLoader.LoadByFilePath(filePath);
        }

        public XnaSound PopSoundBuffer()
        {
            Debug.WriteLine("{0}.PlaySound()", GetType().Namespace, null);

            return Mapper.Map<XnaSound>(_soundPlayer.PopSoundBuffer());
        }
    }
}
